// import axios from "axios";
const axios = require("axios");
const https = require("https");
const getToken = require("../utils/getToken");

let { token, port } = getToken();

const req = axios.create({
  baseURL: `https://127.0.0.1:${port}`,
  httpsAgent: new https.Agent({
    rejectUnauthorized: false,
  }),
  // timeout: 1000,
  auth: {
    username: "riot",
    password: token,
  },
});

// 请求拦截器
req.interceptors.request.use((config) => {
  let { token, port } = getToken();
  config.baseURL = `https://127.0.0.1:${port}`;

  config.auth.password = token;
  return config;
});

// 相应拦截器
req.interceptors.response.use(
  (res) => {
    return res.data;
  },
  (error) => {
    let config = error.config;

    if (error && error.response && error.response.data) {
      return { error: error.response.data };
    } else if (config && config.retry) {
      return { error: "请求超时" };
    } else {
      return { error };
    }
  }
);
module.exports = req;

// req({
//   method: "GET",
//   url: `${base}/admin`,
//   auth: { username: params },
// });

module.exports = req;
